package main.java.Map;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class MapDemo3 {
    static int i=0;
    public static void main(String[] args) throws InterruptedException {
        //HashMap线程不安全
        HashMap<String,Integer> map1=new HashMap<>();
        map1.put("A",40);
        map1.put("AA",60);
        map1.put("BC",80);
        map1.put("D",100);
        map1.put("FE",20);
        System.out.println(map1);
        //将线程不安全的map对象转换为线程安全的map对象
        Map<String, Integer> map2 = Collections.synchronizedMap(map1);
        //LinkedHashMap(可以记录key的添加顺序)
        LinkedHashMap<String,Integer> map3=new LinkedHashMap<>();
        map3.put("D",100);
        map3.put("C",80);
        map3.put("B",60);
        map3.put("A",40);
        System.out.println(map3);
        //TreeMap(这个map可以对添加的元素进行排序,key需要实现Comparable接口
        // 或者构建TreeMap对象时，指定Comparator对象设计排序逻辑)
        TreeMap<Integer,String> treeMap=new TreeMap<>();
        treeMap.put(10,"A");
        treeMap.put(50,"B");
        treeMap.put(20,"C");
        treeMap.put(30,"D");
        System.out.println(treeMap);
        //线程安全的HashMap(可以在并发环境下使用)
        ConcurrentHashMap<String,Integer> map4=new ConcurrentHashMap<>();
        map4.put("A",100);
        map4.put("B",200);
        map4.put("C",300);
        map4.get("D");
        System.out.println(map4);
    }
}
